package com.zking.dao;

import com.zking.entity.PageManager;
import com.zking.entity.Student;
import com.zking.entity.Teacher;
import com.zking.utils.DBPoolUtils;
import jdk.nashorn.internal.ir.TryNode;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

public class TeacherDao extends BaseDao{

    private static QueryRunner runner = new QueryRunner(DBPoolUtils.getDataSource());

    /**
     * 获取总数据数
     */
    public int getTeacherTotal (){
        String sql = "select count(*) from sims_teacher";
        return queryScalarForInteger(sql);
    }

    /**
     * 分页查询
     * @param pm
     * @return
     */
    public List<Teacher> queryTeacher(PageManager pm){
        String sql = "select * from sims_teacher limit ?,?";
        Object[] params = {pm.getStart() , pm.getRn()};
        try {
            return queryObjects(sql, Teacher.class,params);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 添加新老师用户
     * @param t
     * @return
     */
    public int addTea(Teacher t){
        //用户创建时，自己添加姓名，昵称，身份证号，手机号，邮箱
        String sql = "insert into sims_teacher(TEACHER_NAME,NICK_NAME,ID_CARD_NO,MOBILE_PHONE,EMAIL)"
        +"values(?,?,?,?,?)";
        Object[] params ={t.getTEACHER_NAME(),t.getNICK_NAME(),t.getID_CARD_NO(),t.getMOBILE_PHONE(),t.getEMAIL()};
        try {
            return runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除老师用户
     * @param id
     * @return
     */
    public int delTea(int id){
        String sql = "delete from sims_teacher where id = ?";
        try {
            return runner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询一个用户的数据
     * @param id
     * @return
     */
    public Teacher queryTea(int id){
        String sql = "select * from sims_teacher where id = ?";
        try {
            return runner.query(sql,new BeanHandler<>(Teacher.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用户自己更改除了消费金额和积分以外的信息
     * @param t
     * @return
     */
    public int updateTea(Teacher t){
        String sql = "update sims_teacher set TEACHER_NAME=?, NICK_NAME=?, ID_CARD_NO=?, MOBILE_PHONE=?, MONTHLY_SALARY=?, " +
                "HEIGHT=?, WEIGHT=?, NATION=?, DOMICILE_PLACE_PROVINCE=?, DOMICILE_PLACE_CITY=?, DOMICILE_PLACE_ADDRESS=?," +
                "HOBBY=?, PRESENT_ADDRESS=?, EMAIL=?, GENDER=?, BIRTH=?, GRADUATE_INSTITUTION=?, PRACTICE_YEARS=?, POLITICAL=?," +
                "MARITAL=?, AVATAR=?, INTRO=? where TEACHER_ID =?";
        Object[] params = {t.getTEACHER_NAME(),t.getNICK_NAME(),t.getID_CARD_NO(),t.getMOBILE_PHONE(),t.getTOTAL_COST_AMT(),t.getHEIGHT(),t.getWEIGHT(),t.getNATION(),
                t.getDOMICILE_PLACE_PROVINCE(),t.getDOMICILE_PLACE_CITY(),t.getDOMICILE_PLACE_ADDRESS(),t.getHOBBY(),t.getPRESENT_ADDRESS(),t.getEMAIL(),t.getGENDER(),
                t.getBIRTH(),t.getGRADUATE_INSTITUTION(),t.getPRACTICE_YEARS(),t.getPOLITICAL(),t.getMARITAL(),t.getAVATAR(),t.getINTRO(),t.getTEACHER_ID()};
        try {
            return runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
